import React, { Component } from 'react'
import { Icon, Button, Table, Switch, Form, Input, Popconfirm } from 'antd'
import { IndexLink, Link } from 'react-router'
import AddInput from './AddInput'
import EditableCell from './EditableCell'


const FormItem = Form.Item;


export class WilddogUp extends Component {
  constructor(props) {
    super(props);
    this.columns = [{
      title: '活动名称',
      dataIndex: 'name',
      width: '20%',
      render: text => <span className="cellItem_name">{text}</span>,
    }, {
      title: '创建时间',
      dataIndex: 'createdAt',
      sorter: (a, b) => a.createdAt - b.createdAt,
      render: (text) => {
        let newDate = new Date();
        newDate.setTime(text);
        return <p>{newDate.toLocaleDateString("zh-cn",{weekday: "long", year: "numeric", month: "long", day: "numeric","hour": "numeric","minute":"numeric"})}</p>
      },
    }, {
      title: '点赞数',
      dataIndex: 'count',
      render: (text, record, index) => (
        <div>
         {this.props.auth.status?
        <EditableCell
          value={record.count}
          record={record}
          handUpdate={this.updateUpCountHandler}
        />:
        <span className="cellItem_upcount">
        {record.count}</span>
      }
      </div>)
    }, {
      title: '调用key',
      dataIndex: 'id',
    }, {
      title: '操作',
      dataIndex: '',
      key: 'x',
     render: (text, record,index) => (
        <span>
        {this.props.auth.status?
           <Popconfirm title="确认删除咩?" onConfirm={this.deleteOneUpHandler(record.id)}>
              <a href="#">Delete</a>
            </Popconfirm>:'需登录'
            }  
          </span>
      )
    }]
    this.onChange = this.onChange.bind(this);
    this.addOneUpHandler = this.addOneUpHandler.bind(this)
    this.updateUpCountHandler = this.updateUpCountHandler.bind(this)
    this.deleteOneUpHandler = this.deleteOneUpHandler.bind(this)
  }


  componentWillMount() {
    this.props.getUpList();

  }
  addOneUpHandler(name) {
    let { auth } = this.props;
    let payload = {
      createdByUserId: auth.uid,
      name: name,
      type: 'public',
      count: 0
    }
    this.props.addOneUp(payload)
  }
  updateUpCountHandler(valueObj, count) {
    valueObj = Object.assign(valueObj, { count: count });
    console.log(valueObj)
    this.props.updateUpCount(valueObj)
  }
  deleteOneUpHandler(id){
    return ()=>{
      this.props.deleteOneUp(id);
    }
  }
  onChange(pagination, filters, sorter) {
    // console.log('params', pagination, filters, sorter);
  }
  render() {
    // console.log(this.props)
    let { auth } = this.props;
    let { upData ,upFetching} = this.props.wilddog;


    return (
      <div id="wilddog-up">
            <div className="container-wilddog">
             {auth.status?<AddInput  handAdd={this.addOneUpHandler} />:<h2></h2>}
              
              <Table 
              loading={upFetching} pagination={{ pageSize: 5 }}  columns={this.columns} dataSource={upData} onChange={this.onChange} />
            </div>
        
      </div>

    )
  }
}


export default WilddogUp
